﻿#include "practice.h"

Practice::Practice()
{
  viewer = new osgQOpenGLWidget;   
}

Practice::~Practice()
{

}

void Practice::init()
{

}

void Practice::onViewerInitialized()
{
  
}

osg::ref_ptr<osg::Geode> Practice::createQuad()
{
    // 四边形的顶点坐标
    osg::ref_ptr<osg::Vec3Array> vetrices = new osg::Vec3Array;
    vetrices->push_back(osg::Vec3(0,0,0));
    vetrices->push_back(osg::Vec3(1,0,0));
    vetrices->push_back(osg::Vec3(1,0,1));
    vetrices->push_back(osg::Vec3(0,0,1));

    // 设置法线向量
    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
    normals->push_back(osg::Vec3(0,-1.0f,0));

    // 顶点颜色
    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
    colors->push_back(osg::Vec4(1,0,0,1));
    colors->push_back(osg::Vec4(0,1,0,1));
    colors->push_back(osg::Vec4(0,0,1,1));
    colors->push_back(osg::Vec4(1,1,1,1));

    // 创建一个几何图元对象，应用上述的顶点、法线以及颜色参数
    osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
    quad->setVertexArray(vetrices.get());
    quad->setNormalArray(normals.get());
    quad->setNormalBinding(osg::Geometry::BIND_OVERALL);
    quad->setColorArray(colors.get());
    quad->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));

    osg::ref_ptr<osg::Geode> retNode = new osg::Geode;
    retNode->addDrawable(quad.get());
    return retNode;
}